:root {
  // Colors
  --ss-primary-color: #5897fb;
  --ss-bg-color: #ffffff;
  --ss-font-color: #4d4d4d;
  --ss-placeholder-color: #8d8d8d;
  --ss-disabled-color: #dcdee2;
  --ss-border-color: #dcdee2;
  --ss-highlight-color: #fffb8c;
  --ss-focus-color: var(--ss-primary-color);
  --ss-success-color: #00b755;
  --ss-error-color: #dc3545;

  // Heights
  --ss-main-height: 40px;
  --ss-content-height: 300px;
  --ss-search-height: 40px;
  --ss-option-height: auto;

  // Spacing
  --ss-spacing-l: 9px;
  --ss-spacing-m: 7px;
  --ss-spacing-s: 5px;

  // Misc
  --ss-animation-timing: 0.2s;
  --ss-border-radius: 4px;
}

// Animation keyframes
@keyframes ss-valueIn {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
@keyframes ss-valueOut {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

// Misc classes
.ss-hide {
  display: none !important;
}

// Main container
.ss-main {
  display: flex;
  flex-direction: row;
  position: relative;
  user-select: none;
  color: var(--ss-font-color);
  min-height: var(--ss-main-height);
  width: 100%;
  padding: var(--ss-spacing-s);
  cursor: pointer;
  border: 1px solid var(--ss-border-color);
  border-radius: var(--ss-border-radius);
  background-color: var(--ss-bg-color);
  outline: 0;
  box-sizing: border-box;
  transition: background-color var(--ss-animation-timing);
  overflow: hidden;

  &:focus {
    box-shadow: 0 0 5px var(--ss-focus-color);
  }

  &.ss-disabled {
    background-color: var(--ss-disabled-color);
    cursor: not-allowed;

    .ss-values {
      .ss-disabled {
        color: var(--ss-font-color);
      }

      .ss-value {
        .ss-value-delete {
          cursor: not-allowed;
        }
      }
    }
  }

  &.ss-dir-above {
    border-top-left-radius: 0px;
    border-top-right-radius: 0px;
  }
  &.ss-dir-below {
    border-bottom-left-radius: 0px;
    border-bottom-right-radius: 0px;
  }

  .ss-values {
    display: inline-flex;
    flex-wrap: wrap;
    gap: var(--ss-spacing-m);
    flex: 1 1 100%;
    min-width: 0; // Allow container to shrink
    max-width: 100%; // Ensure container doesn't exceed parent
    overflow: hidden; // Prevent overflow

    .ss-placeholder {
      display: block; // Use block for proper ellipsis
      width: 100%;
      min-width: 0; // Allow placeholder to shrink
      padding: var(--ss-spacing-s) var(--ss-spacing-m) var(--ss-spacing-s) var(--ss-spacing-m);
      margin: auto 0px auto 0px;
      line-height: 1em;
      color: var(--ss-placeholder-color);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    // Max is used to display max number of values
    .ss-max {
      display: flex;
      user-select: none;
      align-items: center;
      width: fit-content;
      font-size: 12px;
      color: var(--ss-bg-color);
      line-height: 1;
      padding: var(--ss-spacing-s) var(--ss-spacing-m);
      background-color: var(--ss-primary-color);
      border-radius: var(--ss-border-radius);
    }

    // Used for single select selected value
    .ss-single {
      display: block; // Use block for proper ellipsis
      margin: auto 0px auto var(--ss-spacing-s);
      min-width: 0; // Allow flex item to shrink below content size
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap; // Prevent text wrapping
      flex: 1 1 auto; // Allow it to grow but also shrink
      max-width: calc(100% - 20px); // Leave space for arrow and padding
    }

    // Used for the selected values
    .ss-value {
      display: flex;
      user-select: none;
      align-items: center;
      width: fit-content;
      max-width: 100%; // Ensure value doesn't exceed container width
      background-color: var(--ss-primary-color);
      border-radius: var(--ss-border-radius);
      animation-name: ss-valueIn;
      animation-duration: var(--ss-animation-timing);
      animation-timing-function: ease-out;
      animation-fill-mode: both;

      &.ss-value-out {
        animation-name: ss-valueOut;
        animation-duration: var(--ss-animation-timing);
        animation-timing-function: ease-out;
      }

      .ss-value-text {
        flex: 1; // Take up available space in flex container
        min-width: 0; // Allow text to shrink
        font-size: 12px;
        line-height: 1;
        color: var(--ss-bg-color);
        padding: var(--ss-spacing-s) var(--ss-spacing-m);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .ss-value-delete {
        display: flex;
        align-items: center;
        height: var(--ss-spacing-l);
        width: var(--ss-spacing-l);
        padding: var(--ss-spacing-s) var(--ss-spacing-m);
        cursor: pointer;
        border-left: solid 1px var(--ss-bg-color);
        box-sizing: content-box;

        svg {
          height: var(--ss-spacing-l);
          width: var(--ss-spacing-l);

          path {
            fill: none;
            stroke: var(--ss-bg-color);
            stroke-width: 18;
            stroke-linecap: round;
            stroke-linejoin: round;
          }
        }
      }
    }
  }

  .ss-deselect {
    flex: 0 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: fit-content;
    height: auto;
    padding: 0 var(--ss-spacing-m) 0 var(--ss-spacing-m);

    svg {
      width: 8px;
      height: 8px;

      path {
        fill: none;
        stroke: var(--ss-font-color);
        stroke-width: 20;
        stroke-linecap: round;
        stroke-linejoin: round;
      }
    }
  }

  .ss-arrow {
    flex: 0 1 auto;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    width: 12px;
    height: 12px;
    margin: auto var(--ss-spacing-m) auto var(--ss-spacing-m);

    path {
      fill: none;
      stroke: var(--ss-font-color);
      stroke-width: 18;
      stroke-linecap: round;
      stroke-linejoin: round;
      transition-timing-function: ease-out;
      transition: var(--ss-animation-timing);
    }
  }
}

// Content container
.ss-content {
  position: absolute; // default position
  display: flex;
  height: auto;
  flex-direction: column;
  width: auto;
  max-height: var(--ss-content-height);
  box-sizing: border-box;
  border: solid 1px var(--ss-border-color);
  background-color: var(--ss-bg-color);
  transition:
    transform var(--ss-animation-timing),
    opacity var(--ss-animation-timing);
  opacity: 0;
  transform: scaleY(0);
  overflow: hidden;
  z-index: 10000;

  &.ss-relative {
    position: relative;
    height: 100%;
  }

  &.ss-fixed {
    position: fixed;
  }

  &.ss-dir-above {
    transform-origin: center bottom;
    flex-direction: column-reverse;
    border-top-left-radius: var(--ss-border-radius);
    border-top-right-radius: var(--ss-border-radius);
  }

  &.ss-dir-below {
    transform-origin: center top;
    border-bottom-left-radius: var(--ss-border-radius);
    border-bottom-right-radius: var(--ss-border-radius);
  }

  &.ss-open {
    opacity: 1;
    transform: scaleY(1);
  }

  .ss-search {
    flex: 0 1 auto;
    display: flex;
    flex-direction: row;
    border-bottom: 1px solid var(--ss-border-color);

    input {
      display: inline-flex;
      font-size: inherit;
      line-height: inherit;
      flex: 1 1 auto;
      width: 100%;
      min-width: 0px;
      height: var(--ss-search-height);
      padding: var(--ss-spacing-m) var(--ss-spacing-l);
      margin: 0;
      border: none;
      background-color: var(--ss-bg-color);
      outline: 0;
      text-align: left;
      box-sizing: border-box;

      &::placeholder {
        color: var(--ss-placeholder-color);
        vertical-align: middle;
      }

      &:focus {
        box-shadow: none;
      }
    }

    .ss-addable {
      display: inline-flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      flex: 0 0 auto;
      height: auto;
      margin: 0 0 0 var(--ss-spacing-m);
      border: 1px solid var(--ss-border-color);
      border-radius: var(--ss-border-radius);

      svg {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        flex: 0 1 auto;
        width: 12px;
        height: 12px;
        margin: auto var(--ss-spacing-m) auto var(--ss-spacing-m);

        path {
          fill: none;
          stroke: var(--ss-font-color);
          stroke-width: 18;
          stroke-linecap: round;
          stroke-linejoin: round;
        }
      }
    }
  }

  &.ss-dir-above .ss-search {
    border-bottom: none;
    border-top: 1px solid var(--ss-border-color);
  }

  .ss-list {
    flex: 1 1 auto;
    height: auto;
    overflow-x: hidden;
    overflow-y: auto;

    .ss-error {
      color: var(--ss-error-color);
      padding: var(--ss-spacing-l);
    }

    .ss-searching {
      color: var(--ss-font-color);
      padding: var(--ss-spacing-l);
    }

    .ss-optgroup {
      .ss-option {
        transition:
          max-height var(--ss-animation-timing) ease,
          opacity var(--ss-animation-timing) ease,
          padding-top var(--ss-animation-timing) ease,
          padding-bottom var(--ss-animation-timing) ease,
          margin-top var(--ss-animation-timing) ease,
          margin-bottom var(--ss-animation-timing) ease;
      }

      &.ss-open {
        .ss-option {
          max-height: 100px;
          opacity: 1;
          overflow: visible;
        }
      }

      // If optgroup has close, hide all options
      &.ss-close {
        .ss-option {
          padding-top: 0 !important;
          padding-bottom: 0 !important;
          margin-top: 0 !important;
          margin-bottom: 0 !important;
          max-height: 0;
          opacity: 0;
          overflow: hidden;
        }
      }

      .ss-optgroup-label {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: var(--ss-spacing-m) var(--ss-spacing-l) var(--ss-spacing-m) var(--ss-spacing-l);

        .ss-optgroup-label-text {
          flex: 1 1 auto;
          font-weight: bold;
          color: var(--ss-font-color);
        }

        // If optgroup has arrow, make label clickable
        &:has(.ss-arrow) {
          cursor: pointer;
        }

        .ss-optgroup-actions {
          flex: 0 1 auto;
          display: flex;
          flex-direction: row;
          align-items: center;
          justify-content: center;
          gap: var(--ss-spacing-m);

          .ss-selectall {
            flex: 0 0 auto;
            display: flex;
            flex-direction: row;
            cursor: pointer;

            &:hover {
              opacity: 0.5;
            }

            &.ss-selected {
              svg {
                path {
                  stroke: var(--ss-error-color);
                }
              }
            }

            span {
              flex: 0 1 auto;
              display: flex;
              align-items: center;
              justify-content: center;
              font-size: calc(100% * 0.6);
              text-align: center;
              padding: 0 var(--ss-spacing-s) 0 0;
            }

            svg {
              flex: 0 1 auto;
              width: 13px;
              height: 13px;

              path {
                fill: none;
                stroke: var(--ss-success-color);
                stroke-linecap: round;
                stroke-linejoin: round;
              }

              &:first-child {
                stroke-width: 5;
              }
              &:last-child {
                stroke-width: 11;
              }
            }
          }

          .ss-closable {
            flex: 0 1 auto;
            display: flex;
            flex-direction: row;
            cursor: pointer;

            .ss-arrow {
              flex: 1 1 auto;
              width: 10px;
              height: 10px;

              path {
                fill: none;
                stroke: var(--ss-font-color);
                stroke-width: 18;
                stroke-linecap: round;
                stroke-linejoin: round;
                transition-timing-function: ease-out;
                transition: var(--ss-animation-timing);
              }
            }
          }
        }
      }

      .ss-option {
        padding: var(--ss-spacing-s) var(--ss-spacing-s) var(--ss-spacing-s) calc(var(--ss-spacing-l) * 3);
      }
    }

    .ss-option {
      display: flex;
      align-items: center;
      min-height: var(--ss-option-height);
      padding: var(--ss-spacing-m) var(--ss-spacing-l) var(--ss-spacing-m) var(--ss-spacing-l);
      white-space: normal;
      color: var(--ss-font-color);
      cursor: pointer;
      user-select: none;

      &:hover:not(.ss-disabled) {
        color: var(--ss-bg-color);
        background-color: var(--ss-primary-color);
        border-left: 5px solid var(--ss-primary-color);
        transition: border-left var(--ss-animation-timing) ease;
      }

      &.ss-highlighted,
      &:not(.ss-disabled).ss-selected {
        color: var(--ss-bg-color);
        background-color: var(--ss-primary-color);
      }

      &.ss-disabled {
        cursor: not-allowed;
        background-color: var(--ss-disabled-color);

        &:hover {
          color: var(--ss-font-color);
        }
      }

      // Sub div wrapper for highlighting text
      .ss-search-highlight {
        display: inline-block;
        background-color: var(--ss-highlight-color);
      }
    }

    .ss-search {
      display: flex;
      flex-direction: row;
      align-items: center;
      padding: var(--ss-spacing-m) var(--ss-spacing-l) var(--ss-spacing-m) var(--ss-spacing-l);
    }
  }
}
